package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.FarmerDetailsActivity;
import com.hemaapp.yjnh.activity.MyFarmerActivity;
import com.hemaapp.yjnh.bean.Client;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 田家列表
 */
public class FarmerListAdapter extends BaseAdapter {

    private ArrayList<Client> mDatas = new ArrayList<Client>();// 推荐田家
    private int imgHeight = 0;
    private int tenDp = 0;

    public FarmerListAdapter(Context context, ArrayList<Client> recommendClents) {
        super(context);
        this.mDatas = recommendClents;
        tenDp = BaseUtil.dip2px(context, 10);
    }

    @Override
    public int getCount() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0 ? 1 : mDatas.size();
    }

    @Override
    public boolean isEmpty() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_farmer_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setData(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.ratingBar)
        RatingBar ratingBar;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.layout_contain)
        FrameLayout layoutFrame;

        int position = -1;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (position == -1)
                        return;
                    Intent intent = new Intent();
                    intent.putExtra("client_id", mDatas.get(position).getClient_id());
                    intent.setClass(mContext, FarmerDetailsActivity.class);
                    User user = BaseApplication.getInstance().getUser();
                    if (user != null) {
                        String id = user.getId();
                        if (id.equals(mDatas.get(position).getClient_id())) {
                            intent.setClass(mContext, MyFarmerActivity.class);
                        }
                    }
                    mContext.startActivity(intent);
                }
            });
        }

        private void setData(int position) {
            this.position = position;
            Client client = mDatas.get(position);
            ImageUtils.loadAvatarWithDefault(mContext, client.getAvatar(),ivAvatar, R.drawable.default_avatar_farmer);
            if (client.getItems().size() > 0)
                ImageUtils.loadRadiusImageMid(mContext, client.getItems().get(0).getimgurlbig(), ivBkg, 10);
//            if (position == 0) {
//                ivBkg.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//                imgHeight = ivBkg.getLayoutParams().width*19/58;
//            }
            if (imgHeight > 0) {
                ivBkg.getLayoutParams().height = imgHeight;
                layoutFrame.getLayoutParams().height = imgHeight+5*tenDp;
            }
            String star = client.getStarscore();
            if (!TextUtils.isEmpty(star)) {
                ratingBar.setRating(Float.parseFloat(star));
            }
            tvName.setText(client.getNickname());
            if (isNull(client.getGood_names()))
                tvContent.setText("田家没有提供商品定制");
            else
                tvContent.setText("田家推出"+client.getGood_names()+"等商品定制");
        }
    }
}
